from sqlalchemy import JSON, Column, DateTime, Float, Integer, String, Text, func, text

from databases.v2 import Base


class CodeModel(Base):
    __tablename__ = "code"
    __table_args__ = {
        "comment": "秘钥",
        "mysql_engine": "MyISAM",
        "mysql_collate": "utf8_general_ci",
    }

    id = Column(Integer, primary_key=True, autoincrement=True)
    code = Column(String(50), nullable=False)
    prefix = Column(String(32), nullable=False)
    code_id = Column(Integer, nullable=False)
    status = Column(Integer, nullable=True, server_default=text("1"))
    created_time = Column(DateTime, nullable=True, default=func.now())
    draw_time = Column(DateTime, nullable=True)
    activity_id = Column(Integer, nullable=True)
    prize_id = Column(Integer, nullable=True)
    prize_index = Column(Integer, nullable=True)
    code_batch_id = Column(Integer, nullable=True)


class CodeBatchModel(Base):
    __tablename__ = "code_batch"

    id = Column(Integer, primary_key=True, autoincrement=True)
    prefix = Column(String(32), nullable=True, comment="前缀")
    begin_num = Column(Integer, nullable=False, default=0)
    end_num = Column(Integer, nullable=False, default=0)
    status = Column(Integer, nullable=False, default=0)
    remark = Column(Text, nullable=True)
    file_path = Column(String(255), nullable=True)
    created_time = Column(DateTime, nullable=True, default=func.now())


class PrizeModel(Base):
    __tablename__ = "prize"
    __table_args__ = {
        "comment": "奖品",
        "mysql_engine": "MyISAM",
        "mysql_collate": "utf8_general_ci",
    }

    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(50), nullable=False)
    img = Column(String(2048), nullable=False)
    num = Column(Integer, nullable=True)
    status = Column(Integer, nullable=True, server_default=text("1"))
    is_deleted = Column(Integer, nullable=True, server_default=text("0"))


class ActivityModel(Base):
    __tablename__ = "activity"

    id = Column(Integer, primary_key=True, autoincrement=True)
    type = Column(
        Integer, nullable=True, default="1", comment="活动类型 1刮刮卡 2链接二维码"
    )
    title = Column(String(50), nullable=False, comment="活动标题")
    merchant_id = Column(Integer, nullable=True, comment="商家id")
    num = Column(Integer, nullable=True, comment="活动名额")
    draw_id = Column(Integer, nullable=True, default=text("1"))
    start_time = Column(DateTime, nullable=True, comment="活动开始时间")
    end_time = Column(DateTime, nullable=True, comment="活动结束时间")
    is_bind_client_phone = Column(
        Integer, nullable=True, comment="是否绑定手机号", server_default=text("1")
    )
    is_bind_client_name = Column(
        Integer, nullable=True, comment="是否绑定姓名", server_default=text("0")
    )
    is_bind_client_address = Column(
        Integer, nullable=True, comment="是否绑定地址", server_default=text("0")
    )
    is_custom_prize_img = Column(
        Integer, nullable=True, comment="是否自定义奖品图片", server_default=text("0")
    )
    day_limit_num = Column(
        Integer, nullable=True, comment="每日限制领取数量，0标识不限制"
    )
    client_limit_num = Column(
        Integer, nullable=True, comment="用户领取数量限制，0标识不限制"
    )
    prefix = Column(String(32), nullable=True, comment="前缀")
    qrcode_img_path = Column(String(255), nullable=True, comment="二维码图片地址")
    start_code_id = Column(Integer, nullable=True, comment="开始码id")
    end_code_id = Column(Integer, nullable=True, comment="结束码id")
    status = Column(Integer, nullable=True, comment="状态", server_default=text("0"))
    lock = Column(
        Integer,
        nullable=True,
        comment="活动锁定0 未锁定，1锁定，锁定不能编辑 活动也作废",
        server_default=text("0"),
    )
    remark = Column(Text, nullable=True, comment="备注")
    rules_desc = Column(Text, nullable=True, comment="规则描述")
    kefu_qr_img = Column(String(2048), nullable=True, comment="客服二维码图片")
    kefu_phone = Column(String(32), nullable=True, comment="客服电话")
    created_time = Column(DateTime, default=func.now(), comment="创建时间")
    updated_time = Column(
        DateTime, default=func.now(), onupdate=func.now(), comment="更新时间"
    )


class ActivityPrizeModel(Base):
    __tablename__ = "activity_prize"
    __table_args__ = {
        "comment": "活动奖品中间表",
        "mysql_engine": "MyISAM",
        "mysql_collate": "utf8_general_ci",
    }

    id = Column(Integer, primary_key=True, autoincrement=True)
    prize_id = Column(Integer, nullable=True, comment="奖品id")
    prize_index = Column(
        Integer, nullable=True, comment="奖品索引==一等奖 这样的序号，谢谢惠顾是0"
    )
    activity_id = Column(Integer, nullable=True, comment="活动id")
    num = Column(Integer, nullable=True, comment="奖品数量")
    remain_num = Column(Integer, nullable=True, comment="剩余数量")
    allot_num = Column(Integer, nullable=True, comment="分配数量")
    prize_level_id = Column(String(32), nullable=True, comment="奖品等级id")
    probability = Column(Float(12), nullable=True, comment="概率")


class DrawModel(Base):
    __tablename__ = "draw"

    id = Column(Integer, primary_key=True, autoincrement=True)
    title = Column(String(32), nullable=True, comment="标题")
    prize_num = Column(Integer, nullable=True, comment="奖品数量")
    imgs = Column(Text, nullable=True, comment="图片列表")
    img_head = Column(String(50), nullable=True, comment="头图")
    path = Column(String(50), nullable=True, comment="页面路径")
    status = Column(Integer, nullable=True, comment="状态", server_default=text("1"))
    remark = Column(Text, nullable=True, comment="备注")
    desc = Column(Text, nullable=True, comment="描述")


class ActivityOrderModel(Base):
    __tablename__ = "activity_order"
    id = Column(Integer, primary_key=True, autoincrement=True)
    prize_temp = Column(JSON, nullable=True)
    contact = Column(JSON, nullable=True)
    prize = Column(JSON, nullable=True)
    merchant = Column(JSON, nullable=True)
    activity = Column(JSON, nullable=True)
    code = Column(JSON, nullable=True)
    user = Column(JSON, nullable=True)
    status = Column(Integer, default=0, nullable=True)
    remark = Column(Text, nullable=True)
    created_time = Column(DateTime, default=func.now(), comment="创建时间")
    updated_time = Column(
        DateTime, default=func.now(), onupdate=func.now(), comment="更新时间"
    )


class PrizeTempModel(Base):
    __tablename__ = "prize_temp"
    __table_args__ = {
        "comment": "缓存用户抽奖信息",
        "mysql_engine": "MyISAM",
        "mysql_charset": "utf8mb4",  # 使用 utf8mb4 以支持更多字符集
        "mysql_collate": "utf8mb4_unicode_ci",  # 使用 utf8mb4_unicode_ci 以支持更多的字符排序
    }

    id = Column(Integer, primary_key=True, autoincrement=True, comment="自增id")
    user_id = Column(Integer, nullable=False, comment="用户ID")
    prize_id = Column(Integer, nullable=False, comment="奖品ID")
    prize_index = Column(Integer, nullable=False, comment="奖品ID")
    activity_id = Column(Integer, nullable=False, comment="活动ID")
    status = Column(
        Integer, nullable=False, server_default="0", comment="状态: 0=未抽奖, 1=已抽奖"
    )
    created_time = Column(DateTime, default=func.now(), comment="创建时间")


class ActivityCodeNoModel(Base):
    __tablename__ = "activity_code_no"
    __table_args__ = {
        "comment": "活动绑定的码号编码",
        "mysql_engine": "MyISAM",
        "mysql_charset": "utf8mb4",  # 使用 utf8mb4 以支持更多字符集
        "mysql_collate": "utf8mb4_unicode_ci",  # 使用 utf8mb4_unicode_ci 以支持更多的字符排序
    }

    id = Column(Integer, primary_key=True, autoincrement=True, comment="自增id")
    activity_id = Column(Integer, nullable=False, comment="活动ID")
    code_id_start = Column(Integer, nullable=False, comment="起始codeid")
    code_id_end = Column(Integer, nullable=False, comment="结束code id")
    prefix = Column(String(32), nullable=False, comment="code 前缀")
    total_num = Column(Integer, nullable=False, comment="数量")
